package com.example.cdctravel

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.database.Cursor
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.example.cdctravel.DataBaseHelper.Companion.getInstance
import com.example.cdctravel.R

class LoginActivity : Activity() {
    private var name: EditText? = null
    private var password: EditText? = null
    private var jum: TextView? = null
    private var zhuce: Button? = null
    private var denglu: Button? = null
    private var cb1: CheckBox? = null
    private var cb2: CheckBox? = null
    private var dbHelper: DataBaseHelper? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        //setStatusBar();
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login)
        intiview()
        dbHelper = getInstance(this)
        spauto = false
        checkIfRemember()
        if (Data.sta_dl == true) if (spauto == true) {
            val intent = Intent(this@LoginActivity, MainActivity::class.java)
            Data.sta_np = true
            startActivity(intent)
            finish()
        }
        jum!!.setOnClickListener {
            val intent = Intent(this@LoginActivity, MainActivity::class.java)
            startActivity(intent)
            Data.sta_np = false
            finish()
        }
        zhuce!!.setOnClickListener {
            val intent = Intent(this@LoginActivity, Regiter::class.java)
            startActivity(intent)
        }
        denglu!!.setOnClickListener {
            val db = dbHelper!!.writableDatabase
            val Name = name!!.text.toString()
            val Password = password!!.text.toString()
            if (Name == "") {
                Toast.makeText(this@LoginActivity, "账号不能为空", Toast.LENGTH_SHORT).show()
            } else {
                if (Password == "") {
                    Toast.makeText(this@LoginActivity, "密码不能为空", Toast.LENGTH_SHORT).show()
                } else {
                    val cursor: Cursor
                    cursor = db.rawQuery("select password from users where name=?", arrayOf(Name))
                    if (cursor.moveToNext()) {
                        @SuppressLint("Range") val true_password =
                            cursor.getString(cursor.getColumnIndex("password"))
                        cursor.close()
                        if (true_password == Password) {
                            if (cb1!!.isChecked) {
                                if (cb2!!.isChecked) {
                                    spauto = true
                                    rememberMe(Name, Password, spauto)
                                    Data.sta_dl = true
                                } else {
                                    spauto = false
                                    rememberMe(Name, Password, spauto)
                                } //将用户的帐号与密码存入SharedPreferences
                            } else {
                                spauto = false
                                rememberMe(null, null, spauto)
                            }
                            Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT)
                                .show()
                            var intent: Intent? = null
                            intent = if ("admin" == Name) {
                                Intent(this@LoginActivity, New1::class.java)
                            } else {
                                Intent(this@LoginActivity, MainActivity::class.java)
                            }
                            Data.sta_np = true
                            Data.sta_name = Name
                            Data.sta_password = true_password
                            startActivity(intent)
                            finish()
                        } else Toast.makeText(
                            this@LoginActivity,
                            "密码错误，密码应为$true_password",
                            Toast.LENGTH_SHORT
                        ).show()
                    } else Toast.makeText(
                        this@LoginActivity,
                        "无此账号,先去注册一个吧",
                        Toast.LENGTH_SHORT
                    ).show()
                    cursor.close()
                }
            }
        }
    }

    private fun intiview() {
        name = findViewById(R.id.main_zhanghao)
        password = findViewById(R.id.main_mima)
        zhuce = findViewById(R.id.main_zhuce)
        denglu = findViewById(R.id.main_denglu)
        jum = findViewById(R.id.tiaoguo)
        cb1 = findViewById(R.id.Check_Box1)
        cb2 = findViewById(R.id.Check_Box2)
    }

    fun checkIfRemember() {
        val sp = getSharedPreferences(SPdata, MODE_PRIVATE) //获得SharedPreferences
        spname = sp.getString(SPname, null) //取键值对中的帐号值
        spassword = sp.getString(SPpassword, null) //取键值对中的密码值
        spauto = sp.getBoolean(SPauto, false) //取键值对中的密码值
        if (spname != null && spassword != null) {
            Data.sta_name = spname
            Data.sta_password = spassword
            name!!.setText(spname) //给EditText控件赋帐号
            password!!.setText(spassword) //给EditText控件赋密码
            cb1!!.isChecked = true
            if (spauto == true) cb2!!.isChecked = true
        }
    }

    fun rememberMe(name1: String?, password1: String?, auto1: Boolean) {
        val sp = getSharedPreferences(SPdata, MODE_PRIVATE)
        val editor = sp.edit() //获得Editor
        editor.putString(SPname, name1) //将用户的帐号存入Preferences
        editor.putString(SPpassword, password1) //将密码存入Preferences
        editor.putBoolean(SPauto, auto1)
        editor.commit()
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private fun setStatusBar() {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        //注意要清除 FLAG_TRANSLUCENT_STATUS flag
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor =
            resources.getColor(R.color.white) //设置要显示的颜色（Color.TRANSPARENT为透明）
    }

    companion object {
        private const val SPdata = "SPdata"
        private const val SPname = "name"
        private const val SPpassword = "password"
        private const val SPauto = "auto"
        private var spname: String? = null
        private var spassword: String? = null
        private var spauto = false
    }
}
